package com.wc.trainTest.环境治理;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/3/27 23:42
 * @description
 * https://www.lanqiao.cn/courses/46292/learning/?id=3968755&compatibility=false
 */
public class Main {
    /**
     * 二分 + floyd
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110;
    static int[][] g = new int[N][N], L = new int[N][N], tmp = new int[N][N];
    static int n, Q;

    public static void main(String[] args) {
        n = sc.nextInt();
        Q = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                tmp[i][j] = sc.nextInt();
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                g[i][j] = L[i][j] = sc.nextInt();
            }
        }
        int sum = floyd(), res = -1;
        if (sum <= Q){
            int l = 0, r = 10000000;
            while (l < r) {
                int mid = l + r >> 1;
                if (check(mid)) r = mid;
                else l = mid + 1;
            }
            res = l;
        }
        out.println(res);
        out.flush();
    }

    static int floyd() {
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
                }
            }
        }
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) sum += g[i][j];
        }
        return sum;
    }

    static boolean check(int mid) {
        int rp = mid / n, r = mid % n;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) g[i][j] = tmp[i][j];
        }

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                g[i][j] = Math.max(L[i][j], g[i][j] - rp);
                g[j][i] = Math.max(L[j][i], g[j][i] - rp);
            }
        }

        for (int i = 1; i <= r; i++) {
            for (int j = 1; j <= n; j++) {
                g[i][j] = Math.max(L[i][j], g[i][j] - 1);
                g[j][i] = Math.max(L[j][i], g[j][i] - 1);
            }
        }

        int sum = floyd();

        return sum <= Q;
    }

}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

